package com.loongtech.bi.action;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.PostConstruct;


import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.system.BiPlatformManager;

/**
 * 网页查询基类 2，和BaseBiSearchAction的不同主要在：BaseBiSearchAction在更新区服渠道平台时，受现有统计数据限制，
 * 只更新到全部；而Base2BiSearchAction会根据区服渠道平台列表更新
 * @author Marin
 *	@date 2015年09月30日
 */
public abstract class Base2BiSearchAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	/**
	 * 初始化服务器列表和渠道列表
	 */
	@PostConstruct
	public void initData() {
		platformItmes = new LinkedHashMap<>();
		platformItmes.put("全平台", "0");
		for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
			platformItmes.put(p.getName(), p.getId());
		}
	
		channelItmes = new LinkedHashMap<>();
		channelItmes.put("全渠道", "0");
		for (EntityBiChannel c : BiPlatformManager.CHANNEL_LIST) {
			channelItmes.put(c.getName(), c.getId());
		}
		
		serverItmes = new LinkedHashMap<>();
		serverItmes.put("全区服", "0");
		for (EntityBiServer s : BiPlatformManager.SERVER_LIST) {
			serverItmes.put(s.getName(), s.getId());
		}
	}
	
	/**
	 * 选择了某个平台，更新渠道和区服列表
	 */
	public void selectPlatformItems() {
		// 更新queryChannel和channelItmes，queryServer和serverItmes
		List<EntityBiChannel> list = BiPlatformManager.getChannelIdsByPlatformId(queryPlatform);
		channelItmes = new LinkedHashMap<>();
		channelItmes.put("全渠道", "0");
		for (EntityBiChannel c : list) {
			channelItmes.put(c.getName(), c.getId());
		}
		List<EntityBiServer> list1 = BiPlatformManager.getServerIdsByPlatAndChannId(queryPlatform, queryChannel);
		serverItmes = new LinkedHashMap<>();
		serverItmes.put("全区服", "0");
		for (EntityBiServer s : list1) {
			serverItmes.put(s.getName(), s.getId());
		}
		boolean belong = BiPlatformManager.isChannelBelong2Paltform(queryPlatform, queryChannel);  // 当前queryChannel，是否在queryPlatform范围内，如果没在，则更新queryChannel，如果在，则不变
		if (!belong) {
			queryChannel = "0";
			queryServer = "0";
		}
	}
	
	/**
	 * 选择了某个渠道，更新平台和区服列表
	 */
	public void selectChannelItems() {
		// 更新queryPlatform，queryServer和serverItmes
		queryPlatform = "0".equals(queryChannel) ? queryPlatform : BiPlatformManager.CHANNEL_PLATFORM_MAP.get(queryChannel);
		
		List<EntityBiServer> list1 = BiPlatformManager.getServerIdsByPlatAndChannId(queryPlatform, queryChannel);
		serverItmes = new LinkedHashMap<>();
		serverItmes.put("全区服", "0");
		for (EntityBiServer s : list1) {
			serverItmes.put(s.getName(), s.getId());
		}
		boolean belong = BiPlatformManager.isServerBelong2Channel(queryChannel, queryServer);
		if (!belong) {
			queryServer = "0";
		}
	}
	
	/**
	 * 选择了某个区服，更新渠道和平台列表
	 */
	public void selectServerItems() {
		// 更新queryPlatform，queryChannel和channelItmes
		//queryChannel = "0".equals(queryServer) ? queryChannel : BiPlatformManager.SERVER_MAP.get(queryServer).getChannelIdList().get(0);
		//queryPlatform = "0".equals(queryChannel) ? queryPlatform : BiPlatformManager.CHANNEL_PLATFORM_MAP.get(queryChannel);
		
		/*channelItmes = new LinkedHashMap<>();
		channelItmes.put("全渠道", "0");
		for (EntityBiChannel c : list) {
			channelItmes.put(c.getName(), c.getId());
		}*/
	}
	
	/**
	 * 更新渠道下拉列表为queryPlatform下的所有渠道
	 */
	public void updateChannelItmes() {
		List<EntityBiChannel> list = BiPlatformManager.getChannelIdsByPlatformId(queryPlatform);
		channelItmes = new LinkedHashMap<>();
		channelItmes.put("全渠道", "0");
		for (EntityBiChannel c : list) {
			channelItmes.put(c.getName(), c.getId());
		}
		
		updateServerItmes();
	}
	
	/**
	 * 更新区服下拉列表为channelIds下的所有区服
	 * @param channelIds
	 */
	public void updateServerItmes() {
		if (!"0".equals(queryChannel)) {
			platformItmes = new LinkedHashMap<>();
			platformItmes.put("全平台", "0");
			serverItmes = new LinkedHashMap<>();
			serverItmes.put("全区服", "0");
			
			return;
		}
		
		platformItmes = new LinkedHashMap<>();
		platformItmes.put("全平台", "0");
		for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
			platformItmes.put(p.getName(), p.getId());
		}
		
		// List<EntityBiServer> list = BiPlatformManager.getServerIdsByPlatAndChannId(queryPlatform, queryChannel);
		List<EntityBiServer> list = BiPlatformManager.SERVER_LIST;  // TODO 正式情况下，应该是上句，现在的情况是，没有区服渠道关联，所以渠道和平台为0时，直接返回全区服

		serverItmes = new LinkedHashMap<>();
		serverItmes.put("全区服", "0");
		for (EntityBiServer s : list) {
			serverItmes.put(s.getName(), s.getId());
		}
	}
	
	public void selectedServer() {
		if (!"0".equals(queryServer)) {
			platformItmes = new LinkedHashMap<>();
			platformItmes.put("全平台", "0");
			channelItmes = new LinkedHashMap<>();
			channelItmes.put("全渠道", "0");
			
			return;
		}
		
		/*platformItmes = new LinkedHashMap<>();
		platformItmes.put("全平台", "0");
		for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
			platformItmes.put(p.getName(), p.getId());
		}*/
		initData();
	}
}
